package com.neuedu.core;

import com.neuedu.entity.InterviewRecord;
import com.neuedu.service.InterViewService;
import com.neuedu.service.InterviewRecordService;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ChatMessageDeserializer;
import dev.langchain4j.data.message.ChatMessageSerializer;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CharStorage implements ChatMemoryStore {
    @Resource(name = "wyredistemplate")
    RedisTemplate<String, String> redisTemplate;
    @Override
    public List<ChatMessage> getMessages(Object o) {
        if (redisTemplate.hasKey(o.toString())) {
            String value = redisTemplate.opsForValue().get(o);
            return ChatMessageDeserializer.messagesFromJson(value);
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public void updateMessages(Object o, List<ChatMessage> list) {
        redisTemplate.opsForValue().set(o.toString(), ChatMessageSerializer.messagesToJson(list));
    }

    @Override
    public void deleteMessages(Object o) {
        redisTemplate.delete(o.toString());
    }
}
